<template>
	<div>
		<a-form class="ant-advanced-search-form home-form" :form="form" @submit="handleSearch">
			<a-row :gutter="24">
				<a-col :span="8">
					<a-form-item label="部门">
						<a-select :allowClear="true" v-decorator="['departCode']" placeholder="全部" optionFilterProp="children" showSearch>
							<a-select-option v-for="(item, index) in DepartCodeList" :key="index" :value="item.Code">{{ item.Name }}</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-form-item label="年级">
						<a-select :allowClear="true" v-decorator="['grade']" placeholder="全部" optionFilterProp="children" showSearch>
							<a-select-option v-for="(item, index) in GradeList" :key="index" :value="item">{{ item }}</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-form-item label="获取时间"><a-range-picker v-decorator="['data']" /></a-form-item>
				</a-col>
				<a-col :span="8" v-if="currentType=='total'">
					<a-form-item label="是否合格">
						<a-select :allowClear="true" v-decorator="['type']" placeholder="全部" optionFilterProp="children" showSearch>
							<a-select-option v-for="(item, index) in QualifiedTypeList" :key="item.Key" :value="item.Key">{{ item.Value }}</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="8" style="margin-top: 4px;">
					<a-button-group class="btngroupoption">
						<a-button type="primary" @click="handleSearch">创建任务</a-button>
						<a-button @click="handleReset">重置</a-button>
						<!-- <router-link :to="{ name: 'echartPlatform'}">跳转</router-link> -->
					</a-button-group>
				</a-col>
			</a-row>
		</a-form>
		
		<div class="table">
			<el-table :data="data" v-loading="loading" border highlight-current-row style="width: 100%;">
				<el-table-column type="index" label="序号" width="60"></el-table-column>
				<el-table-column prop="Title" label="任务名称" min-width="100"></el-table-column>
				<el-table-column prop="AddTime" label="创建时间" min-width="140"></el-table-column>
				<el-table-column prop="EndTime" label="结束时间" min-width="140"></el-table-column>
				<el-table-column prop="Progress" label="进度" min-width="140">
					<template slot-scope="scope">
						<div :style="{ color:scope.row.IsError?'red':'' }">{{ scope.row.IsError?'错误提示：'+scope.row.ErrorMsg:scope.row.IsEnd?'已完成':'' }}</div>
						<a-progress v-if="!scope.row.IsError && !scope.row.IsEnd" :percent="parseInt(scope.row.Progress)" />
					</template>
				</el-table-column>
				<el-table-column label="操作" width="120" fixed="right">
					<template slot-scope="scope">
						<div class="tableleft">
							<oprateBtn v-if="scope.row.IsEnd && !scope.row.IsError" title="下载" icon="icon-fujian" color="#1890FF" @tap="downLoad(scope.row, 2)"></oprateBtn>
							<oprateBtn title="删除" icon="icon-delete" color="red" @tap="setList(scope.row, 1)"></oprateBtn>
						</div>
					</template>
				</el-table-column>
			</el-table>
			<pagination @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange" :pagination="pagination"></pagination>
		</div>
		<a-modal title="提示" :visible="visible" @ok="handleOk" :confirmLoading="confirmLoading" @cancel="handleCancel">
			<a-form :form="updateForm" :label-col="{ span: 5 }" :wrapper-col="{ span: 18 }">
				<a-form-item label="任务标题"><a-input v-decorator="['Title', { rules: [{ required: true, message: '请输入任务标题' }] }]" /></a-form-item>
			</a-form>
		</a-modal>
	</div>
</template>

<script>
import pagination from '@components/pagination/index.vue';
import moment from 'moment';
import Cache from '@/utils/cache';
import { GetDepartList, ManageUserScore,ManageUserScoreDoGetManageDepartList } from '@/api/follow';
export default {
	props: {
		currentType: String
	},
	components: {
		pagination
	},
	data() {
		return {
			data: [],
			loading: false,
			form: this.$form.createForm(this, {
				name: 'advanced_search'
			}),
			updateForm:this.$form.createForm(this, {
				name: 'form1'
			}),
			pagination: this.global.pagination,
			values: {},
			DepartCodeList: [],
			GradeList: [],
			QualifiedTypeList: this.global.QualifiedTypeList,
			requestUrl: this.global.requestUrl,
			token: Cache.get('token'),
			setTimeFun: null,
			visible:false,
			confirmLoading:false,
			IsSuper:Cache.get('myInfo').IsSuper,
			DepartCode:Cache.get('myInfo').DepartCode
		};
	},
	created() {
		this.getOtherList();
		this.getGradeList();
		this.getList();
		// if(!this.IsSuper){
		// 	this.form.setFieldsValue({
		// 		'departCode':Cache.get('myInfo').DepartCode
		// 	})
		// }
	},
	destroyed() {
		console.log(3333)
		console.log(this.currentType)
		clearInterval(this.setTimeFun);
	},
	methods: {
		moment,
		async getOtherList() {
			let res = await ManageUserScoreDoGetManageDepartList();
			this.DepartCodeList = res.data.data;
		},
		getGradeList() {
			this.GradeList = [];
			let startYear = 2014;
			let endYear = parseInt(
				moment()
					.add(1, 'y')
					.format('YYYY')
			);
			for (let i = startYear; i < endYear; i++) {
				this.GradeList.push(i);
			}
		},
		handleSearch(e) {
			this.visible = true
		},
		handleReset() {
			this.form.resetFields();
			this.values = {};
			this.getList();
		},
		handleSizeChange(val) {
			this.pagination.pageSize = val;
			this.getList();
		},
		handleCurrentChange(val) {
			this.pagination.currentPage = val;
			this.getList();
		},
		async getList() {
			this.loading = true;
			let data = {};
			data = { pageIndex: this.pagination.currentPage, pageSize: this.pagination.pageSize };
			let res = {};
			if (this.currentType == 'total') {
				res = await ManageUserScore('DoGetExportTotalScoreRecordPageList', data);
			}
			if (this.currentType == 'detail') {
				res = await ManageUserScore('DoGetExportDetailScoreRecordPageList', data);
			}
			this.loading = false;
			this.data = res.data.data;
			const pagination = { ...this.pagination };
			pagination.total = res.data.pageInfo.TotalItemCount;
			this.pagination = pagination;
			let endNum = 0
			for (let i = 0; i < this.data.length; i++) {
				if (this.data[i].IsEnd == false && this.data[i].IsError == false) {
				}else{
					endNum++
				}
			}
			if(endNum != this.data.length){
				clearInterval(this.setTimeFun);
				this.setTimeFun = setInterval(() => {
					this.getList();
				}, 10000);
			}else{
				clearInterval(this.setTimeFun);
			}
		},
		downLoad(text) {
			let hrefUrl = this.requestUrl + 'Manage/UserScore/DownExportFile?userToken=' + encodeURIComponent(this.token) + '&id=' + text.ID;
			window.open(hrefUrl, '_blank');
		},
		setList(text, type) {
			let that = this;
			this.$confirm({
				title: '提示',
				content: type == 1 ? `您确定删除该数据吗？` : `您确定改变该数据的状态吗？`,
				okText: '确认',
				cancelText: '取消',
				async onOk() {
					let data = {};
					data.id = text.ID;
					let res = {};
					res = await ManageUserScore('DoDeleteExportRecord', data);
					that.$message[res.data.code == 0 ? 'success' : 'error'](res.data.msg);
					if (res.data.code == 0) {
						that.getList();
					}
				}
			});
		},
		async handleOk(e) {
			e.preventDefault();
			this.updateForm.validateFields(async (err, values1) => {
				if (!err) {
					this.confirmLoading = true;
					let data = {};
					this.form.validateFields(async (error, values) => {
						if (values.data && values.data.length > 0) {
							values.startTime = values.data[0].format('YYYY-MM-DD');
							values.endTime = values.data[1].format('YYYY-MM-DD');
						}
						delete values.data;
						let data = {};
						data = { ...values,...values1 };
						let res = {};
						if (this.currentType == 'total') {
							res = await ManageUserScore('DoStartExportTotalScore', data);
						}
						if (this.currentType == 'detail') {
							res = await ManageUserScore('DoStartExportDetailScore', data);
						}
						this.$message[res.data.code == 0 ? 'success' : 'error'](res.data.msg);
						if (res.data.code == 0) {
							this.pagination.currentPage = 1;
							this.getList();
						}
					});
					this.updateForm.resetFields();
					this.visible = false;
					this.confirmLoading = false;
				}
			});
		},
		handleCancel(e) {
			this.visible = false;
			this.updateForm.resetFields();
		},
		closeFun(data) {
			this.addIf = false;
			if (data == 1) {
				this.getList();
			}
		}
	}
};
</script>
